﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using System.Data;
using System.Text;

namespace Runda.CMS.Portal.StockManagement
{
    public partial class POList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList(1);
            }
        }

        private string getWhereClause()
        {
            StringBuilder sb = new StringBuilder();
            if (ddlCheckStatus.SelectedValue != "")
            {
                sb.Append(" and (CheckStatus = '" + ddlCheckStatus.SelectedValue + "')");
            }

            if (ddlIsClose.SelectedValue != "")
            {
                sb.Append(" and (IsClose = '" + ddlIsClose.SelectedValue + "')");
            }
            if (ddlstockStatus.SelectedValue != "")
            {
                sb.Append(" and (stockStatus = '" + ddlstockStatus.SelectedValue + "')");
            }
            if (txtContractCodeBasic.Text.Trim() != "")
            {
                sb.Append(" and (PONO like '%" + txtContractCodeBasic.Text.Trim() + "%')");
            }
            if (txtAccessoryCompanyNum.Text.Trim() != "")
            {
                sb.Append(" and POID in (select POID from Cms_Wsm_POS where AccessoryCompanyNumber = '" + txtAccessoryCompanyNum.Text.Trim() + "')");
            }
            return sb.ToString();
        }
        private void BindList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();

            string Whereclause = getWhereClause();
            
            StockBll bll = new StockBll();
            int RecordCount = 0;
            DataTable dt = bll.QueryAccessoryPOMaster(Whereclause, "PODate desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, out RecordCount);

            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        private void BindList1(int pageindex)
        {
            string Whereclause = getWhereClause();

            StockBll bll = new StockBll();
            int RecordCount = 0;

            string whereSql = " and POID='" + hiddenPOID.Value + "'";
            DataTable dt = bll.QueryAccessoryPO(whereSql, "AccessoryName desc", AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex, out RecordCount);

            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager2.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label POID = e.Item.FindControl("POID") as Label;
            Label POSID = e.Item.FindControl("POSID") as Label;
            Label CheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
            Label IsClose = e.Item.FindControl("lblIsClose") as Label;

            if (e.CommandName == "refersh")
            {
                BindList(1);
            }
            if (e.CommandName == "delete")
            {
                new SystemBll().Delete<WsmPOEntity>(" and POID = '" + POID.Text + "'");
                new SystemBll().Delete<WsmPOSEntity>(" and POID = '" + POID.Text + "'");
                BindList(1);
            }

            if (e.CommandName == "view")
            {
                Label StockInID = e.Item.FindControl("POID") as Label;
                hiddenPOID.Value = StockInID.Text;

                BindList1(1);
            }

        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Label POID = e.Item.FindControl("POID") as Label;
            Label POSID = e.Item.FindControl("POSID") as Label;
            
            if (e.CommandName == "refersh")
            {
                BindList1(1);
            }
            
            if (e.CommandName == "close")
            {
                new StockBll().ClosePOS(POSID.Text);
                BindList1(1);
            }

        }

        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {

        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindList(AspNetPager1.CurrentPageIndex);
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {

            BindList1(AspNetPager2.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lblOperatorValue = e.Item.FindControl("lblOperatorValue") as Label;
                Label lblOperator = e.Item.FindControl("lblOperator") as Label;
                LinkButton ltnDel = e.Item.FindControl("ltnDel") as LinkButton;
                LinkButton ltnCheck = e.Item.FindControl("ltnCheck") as LinkButton;
                LinkButton ltnClose = e.Item.FindControl("ltnClose") as LinkButton;
                Label CheckStatus = e.Item.FindControl("lblCheckStatus") as Label;
                Label POID = e.Item.FindControl("POID") as Label;

                if (lblOperator != null && lblOperatorValue != null)
                {
                    SysUserEntity entity = new SysUserBll().Get<SysUserEntity>(new Guid(lblOperatorValue.Text));
                    lblOperator.Text = entity.UserName;
                }

                ltnDel.Visible = false;
                ltnDel.OnClientClick = "return OnDelConfirm();";

                if (base.CheckUserHasFuction(1, "Func6003") && CheckStatus.Text == "0")
                { ltnDel.Visible = true; }

                if (CheckStatus.Text == "0" && base.CheckUserHasFuction(1, "Func6004"))
                {
                    ltnCheck.Text = "审核";
                    ltnCheck.OnClientClick = "window.open('PO.aspx?POID=" + POID.Text + "&checkstatus=0','newwindow','height=500,width=600,top=100,left=200,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no, status=no');";
                    //ltnCheck.OnClientClick = JsExtension.CreateModalWindowScript("PO.aspx?POID=" + POID.Text + "&checkstatus=0", POID.ClientID, 600, 500);
                }
                else if (base.CheckUserHasFuction(1, "Func6004"))
                { 
                    ltnCheck.Text = "弃审";
                    ltnCheck.OnClientClick = "window.open('PO.aspx?POID=" + POID.Text + "&checkstatus=1','newwindow','height=500,width=600,top=100,left=200,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no, status=no');";
                    //ltnCheck.OnClientClick = JsExtension.CreateModalWindowScript("PO.aspx?POID=" + POID.Text + "&checkstatus=1", POID.ClientID, 600, 500);
                }

            }
        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                LinkButton ltnClose = e.Item.FindControl("ltnClose") as LinkButton;
                Label CheckStatus = e.Item.FindControl("lblCheckStatus1") as Label;
                Label POID = e.Item.FindControl("POID") as Label;

                
                if (CheckStatus.Text == "0" && base.CheckUserHasFuction(1, "Func6004"))
                {
                    ltnClose.Visible = true;
                }
                else if (base.CheckUserHasFuction(1, "Func6004"))
                {
                    ltnClose.Visible = false;
                }

                if (CheckStatus.Text == "1")
                { ltnClose.Visible = false; }

            }
        }

        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            string Whereclause = getWhereClause();

            DataTable dt = new StockBll().ExportPOList(Whereclause, " 订购日期");

            Export.Export2Excel(dt, "采购订单一览表", Page.Response);

        }
    }
}
